package com.rich.dmr.utils;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.util.Log;
import com.rich.dmr.R;
import java.io.IOException;

public class SoundPoolUtils {
    private String TAG = "rich---DmrBroadcastReceiver";
    private static SoundPoolUtils mSoundPoolUtils;
    private SoundPool soundPool;
    // Maximumn sound stream.
    private static final int MAX_STREAMS = 5;
    // Stream type.
    private static final int streamType = AudioManager.STREAM_MUSIC;
    private boolean loaded;
    private int soundPTTId;
    private int soundGroupId;
    private int soundErrId;
    private float volume;

    public static SoundPoolUtils getInstance(Context context) {
        if (mSoundPoolUtils == null) {
            synchronized (SoundPoolUtils.class) {
                mSoundPoolUtils = new SoundPoolUtils(context);
            }
        }
        return mSoundPoolUtils;
    }

    public SoundPoolUtils(Context context) {
        // AudioManager audio settings for adjusting the volume
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        // Current volumn Index of particular stream type.
        float currentVolumeIndex = (float) audioManager.getStreamVolume(streamType);
        // Get the maximum volume index for a particular stream type.
        float maxVolumeIndex  = (float) audioManager.getStreamMaxVolume(streamType);
        // Volumn (0 --> 1)
        this.volume = currentVolumeIndex / maxVolumeIndex;
        // Suggests an audio stream whose volume should be changed by
        // the hardware volume controls.
        //context.setVolumeControlStream(streamType);
        // For Android SDK >= 21
        if (Build.VERSION.SDK_INT >= 21 ) {
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_GAME)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build();

            SoundPool.Builder builder= new SoundPool.Builder();
            builder.setAudioAttributes(audioAttributes).setMaxStreams(MAX_STREAMS);

            this.soundPool = builder.build();
        }
        // for Android SDK < 21
        else {
            // SoundPool(int maxStreams, int streamType, int srcQuality)
            this.soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
        }
        // When Sound Pool load complete.
        this.soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                loaded = true;
            }
        });
        // Load sound file (destroy.wav) into SoundPool.
            this.soundPTTId = this.soundPool.load(context, R.raw.ptt_down,1);
            this.soundErrId = this.soundPool.load(context, R.raw.ptt_error,1);
            try {
                this.soundGroupId = this.soundPool.load( context.getAssets().openFd("enterGroup.mp3"),1);
            } catch (IOException e) {
                e.printStackTrace();
            }
           Log.d(TAG,"SoundPoolUtils");
        }


    public void playSound(boolean isRaw)  {
        if(loaded)  {
            // Play sound of gunfire. Returns the ID of the new stream.
            // Play sound objects destroyed. Returns the ID of the new stream.

            if(isRaw){
                int streamId = this.soundPool.play(this.soundPTTId,volume, volume, 1, 0, 1f);
            }else {
                int streamId = this.soundPool.play(this.soundGroupId,volume, volume, 1, 0, 1f);
            }
        }
    }

    public void playSoundErr()  {
        if(loaded)  {
            // Play sound of gunfire. Returns the ID of the new stream.
            // Play sound objects destroyed. Returns the ID of the new stream.
            int streamId = this.soundPool.play(this.soundErrId,volume, volume, 1, 0, 1f);
        }
    }


}
